package com.ssm.demo.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mysql.jdbc.StringUtils;
import com.ssm.demo.model.ManagerUser;
import com.ssm.demo.model.vo.UserVo;
import com.ssm.demo.service.manageruser.IManagerUserService;

/**
 * 
 * @ClassName: ManagerUserController
 * @Description: 管理人员Controller
 * @author Yin Qingsheng
 * @date 2018年12月26日 下午4:06:38
 *
 */
@Controller
@RequestMapping("/managerUser")
public class ManagerUserController {

	/**
	 * 日志
	 */
	private static Logger log = LoggerFactory.getLogger(ManagerUserController.class);

	/**
	 * imanagerUserService
	 */
	@Resource
	private IManagerUserService imanagerUserService;

	/**
	 *  展示全部集合数据
	 * @param model
	 * @param session 当前session
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String list(Model model, HttpSession session,  String queryName, String queryValue) {

		List<ManagerUser> list = imanagerUserService.query(queryName, queryValue);
		model.addAttribute("list", list);
		model.addAttribute("queryName", queryName);
		model.addAttribute("queryValue", StringUtils.isNullOrEmpty(queryName) ? "" : queryValue);
		return "managerUser/list";
	}

	/**
	 * 添加跳转方法
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/add")
	public String add(Model model) {
		return "/managerUser/add";
	}

	/**
	 * 修改方法
	 * @param id 主键
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit/{id}")
	public String edit(@PathVariable("id") Integer id, Model model) {
		model.addAttribute("managerUser", imanagerUserService.find(id));
		return "/managerUser/edit";
	}

	/**
	 * 保存方法
	 * @param managerUser 实体对你
	 * @param ra
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/saveOrUpdate")
	public String save(ManagerUser managerUser, HttpSession session) {
		try {
			UserVo user = (UserVo) session.getAttribute("user");
			if(managerUser.getId() != null){
				imanagerUserService.update(managerUser, user);
			}else {
				imanagerUserService.save(managerUser, user);
			}
			
		} catch (Exception e) {
			log.error("保存发生未知异常", e);
		}
		return "redirect:/managerUser/list";
	}
	
	/**
	 * 删除方法，可批量删除
	 * @param idGroup id数据
	 * @param ra
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/delete")
	public String delete(Integer[] idGroup, HttpSession session) {
		try {
			UserVo userVo = (UserVo) session.getAttribute("user");
			List<Integer> idList = Arrays.asList(idGroup);
			List<ManagerUser> modelList = imanagerUserService.findByIdIn(idList);
			imanagerUserService.deleteInBatch(modelList, userVo);

		} catch (Exception e) {
			log.error("删除发生未知异常", e);
		}
		return "redirect:/managerUser/list";
	}

}
